/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormAgendaEti.java
 *
 * Created on 11/03/2012, 09:51:08
 */
package br.com.ufc.view;

import br.com.ufc.contoller.AgendaArray;
import br.com.ufc.contoller.AgendaDataBase;
import br.com.ufc.contoller.AgendaHash;
import br.com.ufc.contoller.AgendaVector;
import br.com.ufc.contoller.Agendavel;
import br.com.ufc.model.ConfiguracoesSistema;
import java.util.ResourceBundle;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.apache.log4j.Logger;

/**
 *
 * @author Paulo
 */
public class FormAgendaEti extends javax.swing.JFrame {

    private static Logger LOGGER = Logger.getLogger(FormAgendaEti.class);
    private static JFrame form = null;
    public static Agendavel agenda;
    private static ResourceBundle APP_PROP;
    private static ConfiguracoesSistema configuracoesSistema;
    public static String usuario;

    public static void setConfiguracoesSistema(ConfiguracoesSistema configuracoesSistema) {
        FormAgendaEti.configuracoesSistema = configuracoesSistema;
    }

    public static ConfiguracoesSistema getConfiguracoesSistema() {
        return configuracoesSistema;
    }

    /** Creates new form FormAgendaEti */
    public FormAgendaEti() {
        initComponents();
    }

    private void atualizarBarraTarefas() {
        jlDescTipoAgenda.setText((configuracoesSistema != null) ? configuracoesSistema.getTipoAgenda().toString() : "-------");
        jlDescBanco.setText((configuracoesSistema == null ? "-------" : configuracoesSistema.getBancoDados()));
        jlDescUsuario.setText(this.usuario.toUpperCase());
    }

    private void montaFormLogin() {
        FormLogin formLogin = new FormLogin(this, true);
        formLogin.setLocationRelativeTo(null);
        formLogin.setVisible(true);
    }

    private void inicializaComponentes() {
        if (configuracoesSistema == null) {
            montarFormConfiguracao();
        }
        inicializarAgenda();
        atualizarBarraTarefas();
    }

    private void inicializarAgenda() {
        switch (configuracoesSistema.getTipoAgenda().ordinal()) {
            case 0:
                agenda = new AgendaHash();
                break;
            case 1:
                agenda = new AgendaVector();
                break;
            case 2:
                agenda = new AgendaArray();
                break;
            default:
                agenda = new AgendaDataBase();
                break;
        }

        if (!(agenda instanceof AgendaDataBase)) {
            agenda.setTarefas(configuracoesSistema.getTamanhoArray());
        }
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        //montarSplash();
        montaFormLogin();
        inicializaComponentes();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktop = new javax.swing.JDesktopPane();
        jToolBar1 = new javax.swing.JToolBar();
        jlRotuloTipoAgenda = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jlDescTipoAgenda = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jlRotuloBanco = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jlDescBanco = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        jlRotuloUsuario = new javax.swing.JLabel();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        jlDescUsuario = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jmArquivo = new javax.swing.JMenu();
        jmiCadastroTarefa = new javax.swing.JMenuItem();
        jmiListagemTarefas = new javax.swing.JMenuItem();
        jmFerramentas = new javax.swing.JMenu();
        jmiConfiguracoesSistema = new javax.swing.JMenuItem();
        jmAjuda = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jmiSobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });

        jDesktop.setBackground(new java.awt.Color(204, 204, 204));

        jToolBar1.setRollover(true);

        jlRotuloTipoAgenda.setText("Tipo de Tarefa:");
        jToolBar1.add(jlRotuloTipoAgenda);
        jToolBar1.add(jSeparator1);

        jlDescTipoAgenda.setText((configuracoesSistema != null)? configuracoesSistema.getTipoAgenda().toString(): "-------");
        jToolBar1.add(jlDescTipoAgenda);
        jToolBar1.add(jSeparator2);

        jlRotuloBanco.setText("Banco:");
        jToolBar1.add(jlRotuloBanco);
        jToolBar1.add(jSeparator3);

        jlDescBanco.setText((configuracoesSistema == null? "-------":configuracoesSistema.getBancoDados()));
        jToolBar1.add(jlDescBanco);
        jToolBar1.add(jSeparator4);

        jlRotuloUsuario.setText("Usuário:");
        jToolBar1.add(jlRotuloUsuario);
        jToolBar1.add(jSeparator5);

        jlDescUsuario.setText((configuracoesSistema == null? "-------":configuracoesSistema.getBancoDados()));
        jToolBar1.add(jlDescUsuario);

        jmArquivo.setText("Arquivo");

        jmiCadastroTarefa.setText("Cadastro de Tarefa");
        jmiCadastroTarefa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiCadastroTarefaActionPerformed(evt);
            }
        });
        jmArquivo.add(jmiCadastroTarefa);

        jmiListagemTarefas.setText("Listagem de Tarefas");
        jmiListagemTarefas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiListagemTarefasActionPerformed(evt);
            }
        });
        jmArquivo.add(jmiListagemTarefas);

        jMenuBar1.add(jmArquivo);

        jmFerramentas.setText("Ferramentas");

        jmiConfiguracoesSistema.setText("Configurações de Sistema");
        jmiConfiguracoesSistema.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiConfiguracoesSistemaActionPerformed(evt);
            }
        });
        jmFerramentas.add(jmiConfiguracoesSistema);

        jMenuBar1.add(jmFerramentas);

        jmAjuda.setText("Ajuda");

        jMenuItem1.setText("Ajuda");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jmAjuda.add(jMenuItem1);

        jmiSobre.setText("Sobre");
        jmiSobre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmiSobreActionPerformed(evt);
            }
        });
        jmAjuda.add(jmiSobre);

        jMenuBar1.add(jmAjuda);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jDesktop, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jDesktop, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jmiCadastroTarefaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiCadastroTarefaActionPerformed
        FormCadastroTarefa formCadastroTarefa = new FormCadastroTarefa();
        formCadastroTarefa.setLocationRelativeTo(null);
        formCadastroTarefa.setModal(true);
        formCadastroTarefa.setVisible(true);
    }//GEN-LAST:event_jmiCadastroTarefaActionPerformed

    private void jmiListagemTarefasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiListagemTarefasActionPerformed
        FormListagemTarefas formListagemTarefas = new FormListagemTarefas();
        formListagemTarefas.setLocationRelativeTo(null);
        
        formListagemTarefas.setModal(true);
        formListagemTarefas.setVisible(true);
    }//GEN-LAST:event_jmiListagemTarefasActionPerformed

    private void jmiConfiguracoesSistemaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiConfiguracoesSistemaActionPerformed
        montarFormConfiguracao();
    }//GEN-LAST:event_jmiConfiguracoesSistemaActionPerformed

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        atualizarBarraTarefas();
        if ((configuracoesSistema != null) && ((agenda != null) && (configuracoesSistema.getHouveAlteracoesSistema()))) {
            inicializarAgenda();
            manterVisual();
            configuracoesSistema.setHouveAlteracoesSistema(false);
        }
    }//GEN-LAST:event_formFocusGained

    private void jmiSobreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiSobreActionPerformed
        FormSplash formSplash = new FormSplash(this, true);
        formSplash.setLocationRelativeTo(null);
        formSplash.setVisible(true);
    }//GEN-LAST:event_jmiSobreActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        FormAjuda formAjuda = new FormAjuda(this, true);
        formAjuda.setLocationRelativeTo(null);
        formAjuda.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void montarFormConfiguracao() {
        FormConfiguracoesSistema formConfiguracoesSistema = new FormConfiguracoesSistema();
        formConfiguracoesSistema.setLocationRelativeTo(null);
        formConfiguracoesSistema.setModal(true);
        formConfiguracoesSistema.setVisible(true);
    }

    private void manterVisual() {
        try {
            UIManager.setLookAndFeel(configuracoesSistema.getPadraoVisual().getValue());
            this.initComponents();
            this.inicializaComponentes();
            this.repaint();
        } catch (ClassNotFoundException ex) {
            LOGGER.error(ex.getMessage());
        } catch (InstantiationException ex) {
            LOGGER.error(ex.getMessage());
        } catch (IllegalAccessException ex) {
            LOGGER.error(ex.getMessage());
        } catch (UnsupportedLookAndFeelException ex) {
            LOGGER.error(ex.getMessage());
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                    LOGGER.error(ex.getMessage());
                } catch (InstantiationException ex) {
                    LOGGER.error(ex.getMessage());
                } catch (IllegalAccessException ex) {
                    LOGGER.error(ex.getMessage());
                } catch (UnsupportedLookAndFeelException ex) {
                    LOGGER.error(ex.getMessage());
                }
                form = new FormAgendaEti();
                form.setLocationRelativeTo(null);
                form.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane jDesktop;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel jlDescBanco;
    private javax.swing.JLabel jlDescTipoAgenda;
    private javax.swing.JLabel jlDescUsuario;
    private javax.swing.JLabel jlRotuloBanco;
    private javax.swing.JLabel jlRotuloTipoAgenda;
    private javax.swing.JLabel jlRotuloUsuario;
    private javax.swing.JMenu jmAjuda;
    private javax.swing.JMenu jmArquivo;
    private javax.swing.JMenu jmFerramentas;
    private javax.swing.JMenuItem jmiCadastroTarefa;
    private javax.swing.JMenuItem jmiConfiguracoesSistema;
    private javax.swing.JMenuItem jmiListagemTarefas;
    private javax.swing.JMenuItem jmiSobre;
    // End of variables declaration//GEN-END:variables
}
